﻿using Microsoft.AspNetCore.Mvc;
using SMARTHOTEL.Customers;
using SMARTHOTEL.hotel;
using SMARTHOTEL.RoomStatues;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SMARTHOTEL.StayRecords
{
    /// <summary>
    /// 住宿记录服务
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class StayRecordService : ApplicationService, IStayRecordService
    {
        IRepository<StayRecord> recRep;
        IRepository<RoomNumber> roomnumber;
        IRepository<RoomType> roomtype;
        IRepository<Reservations>  reservations;

   

        public StayRecordService(IRepository<StayRecord> recRep, IRepository<RoomNumber> roomnumber, IRepository<RoomType> roomtype, IRepository<Reservations> reservations)
        {
            this.recRep = recRep;
            this.roomnumber = roomnumber;
            this.roomtype = roomtype;
            this.reservations = reservations;
        }

        /// <summary>
        ///添加住宿记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [ HttpPost("InsertStayrecord")]
        public async Task<ResultDto<bool>> InsertStayrecord(CreateUpdateStayRecordDto dto)
        {
            try
            {
                var entity = ObjectMapper.Map<CreateUpdateStayRecordDto, StayRecord>(dto);

                await recRep.InsertAsync(entity);


                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
            }
            catch (Exception ex)
            {

                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败" + ex.Message };
            }
        }
        /// <summary>
        /// 获取住宿记录
        /// </summary>
        /// <param name="StayState"></param>
        /// <param name="ReservationName"></param>
        /// <param name="CheckInTime"></param>
        /// <param name="CheckOutTime"></param>
        /// <returns></returns>
        public async Task<ResultDto<List<StayRecordDto>>> GetStayRecord(int StayState, string? ReservationName, DateTime? CheckInTime, DateTime? CheckOutTime)
        {

            var list = from a in await recRep.GetListAsync()
                       join b in await roomnumber.GetListAsync() on a.RoomNumberId equals b.Id
                       join c in await roomtype.GetListAsync() on b.RoomTypeId equals c.Id
                       select new StayRecordDto
                       {
                           Id = a.Id,
                           SourceInformation = a.SourceInformation,
                           OrderSource = a.OrderSource,
                           ReservationName = a.ReservationName,
                           ReservationCode = a.ReservationCode,
                           CheckInTime = a.CheckInTime,
                           CheckOutTime = a.CheckOutTime,
                           CheckInDayNum = a.CheckInDayNum,
                           RoomTypeId = b.RoomTypeId,
                           RoomTypeName = c.RoomTypeName,
                           RoomNum = b.RoomNum,
                           CustomerTypeId = a.CustomerTypeId,
                           StayState = a.StayState,
                           RoomAmount = a.RoomAmount,
                           RoomCustomizePrice = a.RoomCustomizePrice,
                           TotalPrices = a.TotalPrices,
                           MerchantDesc = a.MerchantDesc,

                       };
            if (StayState != 0)
            {
                list = list.Where(x => x.StayState == (RoomStatus)StayState);
            }
            if (!string.IsNullOrEmpty(ReservationName))
            {
                list = list.Where(x => x.ReservationName.Contains(ReservationName));
            }


            return new ResultDto<List<StayRecordDto>> { Data = list.ToList(), Code = StatusCode.Success, Message = "查询成功" };
        }
        /// <summary>
        /// 修改住宿记录
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> UpdateStaystate(Guid Id)
        {
            var entity = await recRep.FirstOrDefaultAsync(x => x.Id == Id);
            if (entity != null)
            {
                entity.StayState = RoomStatus.已取消;
                await recRep.UpdateAsync(entity);
                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "修改成功" };
            }
            else
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "修改失败" };
            }
        }


        /// <summary>
        /// 获取订房记录
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetReservationRecord")]
        public async Task<ResultDto<List<Reservations>>> GetReservationRecord()
        {


            List<Reservations> list = await reservations.GetListAsync(); // 异步执行查询并将结果转换为列表;



            return new ResultDto<List<Reservations>> { Data = list, Code = StatusCode.Success, Message = "查询成功" };
        }

    }
}
